package gui;

import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.LogIn;
import client.Main;
import client.MemberAccount;
import client.CreditCard;

public class MemberAccountGUI extends JFrame{
	
	//variables
	int[] month={1,2,3,4,5,6,7,8,9,10,11,12};
	String[] year=new String[5];
	MemberAccount ma;
	CreditCard cc=null;
	
	//panels
	JPanel panel;
	
	//combo box
	JComboBox cbExpireDateMonth;
	JComboBox cbExpireDateYear;
	
	//text
	JTextField txtCustomerName;
	JTextField txtCustomerID;
	JTextField txtCustomerCreditCardNumber;
	JTextField txtExpireDate;
	JTextField txtAddress;
	JTextField txtPhone;
	
	//labels
	JLabel lblHeader;
	JLabel lblCustomerName;
	JLabel lblCustomerID;
	JLabel lblCustomerCreditCardNumer;
	JLabel lblExpireDate;
	JLabel lblAddress;
	JLabel lblPhone;
	
	//buttons
	public JButton btnBack;
	JButton btnSubmit;
	
	JComboBox getComboExpireDateMonth()
	{
		if(cbExpireDateMonth==null)
		{
			cbExpireDateMonth=new JComboBox();
			for(int i=0;i<month.length;i++)
				cbExpireDateMonth.addItem(month[i]);
			cbExpireDateMonth.setBounds(250, 240, 50, 20);
		}
		return cbExpireDateMonth;
	}
	
	JComboBox getComboExpireDateYear()
	{
		if(cbExpireDateYear==null)
		{
			cbExpireDateYear=new JComboBox();
			cc=new CreditCard();
			year=cc.getYears();
			for(int i=0;i<year.length;i++)
				cbExpireDateYear.addItem(year[i]);
			cbExpireDateYear.setBounds(320, 240, 70, 20);
		}
		return cbExpireDateYear;
	}
	
	JLabel getLabelExpireDate()
	{
		if(lblExpireDate==null)
		{
			lblExpireDate=new JLabel();
			lblExpireDate.setText("Expire Date:");
			lblExpireDate.setBounds(40, 240, 200, 20);
			lblExpireDate.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblExpireDate;
	}
	
	JLabel getLabelCustomerName()
	{
		if(lblCustomerName==null)
		{
			lblCustomerName=new JLabel();
			lblCustomerName.setText("Name:");
			lblCustomerName.setBounds(40, 80, 200, 20);
			lblCustomerName.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblCustomerName;
	}
	
	JLabel getLabelCustomerID()
	{
		if(lblCustomerID==null)
		{
			lblCustomerID=new JLabel();
			lblCustomerID.setText("ID:");
			lblCustomerID.setBounds(40, 110, 200, 20);
			lblCustomerID.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblCustomerID;
	}
	
	JLabel getLabelCustomerAddress()
	{
		if(lblAddress==null)
		{
			lblAddress=new JLabel();
			lblAddress.setText("Address:");
			lblAddress.setBounds(40, 140, 200, 20);
			lblAddress.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblAddress;
	}
	
	JLabel getLabelCustomerCreditCardNumer()
	{
		if(lblCustomerCreditCardNumer==null)
		{
			lblCustomerCreditCardNumer=new JLabel();
			lblCustomerCreditCardNumer.setText("Credit Card Number:");
			lblCustomerCreditCardNumer.setBounds(40, 210, 200, 20);
			lblCustomerCreditCardNumer.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblCustomerCreditCardNumer;
	}
	
	JLabel getLabelCustomerPhone()
	{
		if(lblPhone==null)
		{
			lblPhone=new JLabel();
			lblPhone.setText("Phone:");
			lblPhone.setBounds(40, 170, 200, 20);
			lblPhone.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblPhone;
	}
	
	JTextField getTextCustomerPhone()
	{
		if(txtPhone==null)
		{
			txtPhone=new JTextField();
			txtPhone.setBounds(250, 170, 200, 20);
		}
		return txtPhone;
	}
	
	JTextField getTextCustomerAddress()
	{
		if(txtAddress==null)
		{
			txtAddress=new JTextField();
			//txtCustomerID.setEnabled(false);
			//txtCustomerID.setText(String.valueOf(LogIn.usr.getID()));
			txtAddress.setBounds(250, 140, 200, 20);
			txtAddress.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return txtAddress;
	}
	
	JTextField getTextCustomerID()
	{
		if(txtCustomerID==null)
		{
			txtCustomerID=new JTextField();
			//txtCustomerID.setEnabled(false);
			//txtCustomerID.setText(String.valueOf(LogIn.usr.getID()));
			txtCustomerID.setBounds(250, 110, 200, 20);
			txtCustomerID.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return txtCustomerID;
	}
	
	

	JTextField getTextCustomerName()
	{
		if(txtCustomerName==null)
		{
			txtCustomerName=new JTextField();
			txtCustomerName.setEnabled(false);
			txtCustomerName.setText(LogIn.usr.getName());
			txtCustomerName.setBounds(250, 80, 200, 20);
			txtCustomerName.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return txtCustomerName;
	}
	
	JTextField getTextCustomerCreditCard()
	{
		if(txtCustomerCreditCardNumber==null)
		{
			txtCustomerCreditCardNumber=new JTextField();
			txtCustomerCreditCardNumber.setText("");
			txtCustomerCreditCardNumber.setBounds(250, 210, 200, 20);
			txtCustomerCreditCardNumber.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return txtCustomerCreditCardNumber;
	}
	
	JButton getButtonSubmit()
	{
		if(btnSubmit==null)
		{
			btnSubmit=new JButton();
			btnSubmit.setText("Submit");
			btnSubmit.setBounds(140, 300, 100, 40);
			btnSubmit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
									
					if(txtCustomerCreditCardNumber.getText().length()>0)
					{
						ma=new MemberAccount(txtCustomerCreditCardNumber.getText(),cbExpireDateMonth.getSelectedItem().toString(),cbExpireDateYear.getSelectedItem().toString(),txtAddress.getText(),txtPhone.getText(),txtCustomerID.getText(),LogIn.usr.getID());

						if(ma.getCreditCard().validateCreditCardNumer()==0)
						{
							JOptionPane.showMessageDialog(null, "Credit Card Number is not valid");
							txtCustomerCreditCardNumber.setText(null);
							return;
						}
						if(ma.getCreditCard().validateExpireDate()==0)
						{
							JOptionPane.showMessageDialog(null, "Credit Card Expire Date is not valid");
							return;
						}
					}
					else
						ma=new MemberAccount(LogIn.usr.getID(),txtPhone.getText(),txtAddress.getText(),txtCustomerID.getText());
					
					if(ma.checkID()==0)
						return;
					
					if(ma.submit()==1)
					{
						JOptionPane.showMessageDialog(null, "Request Sent! Please wait for system manager to approve");
						btnBack.doClick();
					}
					else
						JOptionPane.showMessageDialog(null, "Already listed in member list");
				}
			});
			
		}
		return btnSubmit;
	}
	
	JButton getButtonBack()
	{
		if(btnBack==null)
		{
			btnBack=new JButton();
			btnBack.setText("Back");
			btnBack.setBounds(250, 300, 100, 40);
		}
		return btnBack;
	}
	
	JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("Create Member Account");
			lblHeader.setBounds(this.getWidth()/2-300/2, 20, 300, 20);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
	JPanel getPanel()
	{
		if(panel==null)
		{
			panel=new JPanel();
			panel.setSize(this.getWidth(),this.getHeight());
			panel.setLayout(null);
			
			//labels
			panel.add(getLabelHeader());
			panel.add(getLabelCustomerName());
			panel.add(getLabelCustomerID());
			panel.add(getLabelCustomerCreditCardNumer());
			panel.add(getLabelExpireDate());
			panel.add(getLabelCustomerAddress());
			panel.add(getLabelCustomerPhone());
			
			//text fields
			panel.add(getTextCustomerID());
			panel.add(getTextCustomerName());
			panel.add(getTextCustomerCreditCard());
			panel.add(getTextCustomerAddress());
			panel.add(getTextCustomerPhone());
			
			//combo box
			panel.add(getComboExpireDateMonth());
			panel.add(getComboExpireDateYear());
			
			//buttons
			panel.add(getButtonSubmit());
			panel.add(getButtonBack());
		}
		return panel;
	}
	
	public MemberAccountGUI()
	{
		super();
		init();
	}
	
	void init()
	{
		this.setSize(500,400);
		this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 500, 400);
		this.setTitle("Zer Lah - Create Account Member");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.add(getPanel());
		this.setVisible(true);
	}
}